package com.sx.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.druid.support.json.JSONWriter;
import com.alibaba.fastjson.JSON;
import com.dfec.model.Msg;
import com.dfec.utils.Consists;
import com.dfec.utils.ResponseUtil;
import com.sx.service.ForcastService;

/**
 * Servlet implementation class ForcastController
 */
@WebServlet(name = "forcast", description = "逐小时预报", urlPatterns = { "/forcast" })
public class ForcastController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ForcastService s=new ForcastService();
	private List<Object> list=null;
    Msg msg=null;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		msg=new Msg();
		list=new ArrayList<>();
		List<Object> getdata = null ;
		try {
			String city=request.getParameter("city");
			String cnty=request.getParameter("cnty");
			getdata = s.getForcast(city, cnty);
			if(!getdata.get(0).toString().equals("[]")){
				msg.setCode(Consists.DATA_Code_OK);
				msg.setMsg(Consists.DATA_Msg_OK);
			}else {
				msg.setCode(Consists.DATA_Code_OK__NODATA);
				msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
		}
		list.add(msg);
		list.add(getdata.get(0));
		
	 ResponseUtil.sendScript(response, JSON.toJSONString(list));
	}

}
